System and method for generating a json schema from a json stream

ABSTRACT

In accordance with an embodiment, described herein is a JSON schema generator that parses a JSON stream, determines a data type of each record of the JSON stream, and selects a corresponding record schema generation logic component based on the determined data type to build a record schema for that record. In building a record schema, each element in a record and each sub-element in one or more nested layers of the record can be examined, to determine their data types. A corresponding parser can be selected for each element and sub-element based on a determined data type to generate a schema node. The generated schema nodes can be joined together to create a record schema for the record. Each record schema can be dynamically merged together to create a schema for the JSON stream.

COPYRIGHT NOTICE

A portion of the disclosure of this patent document contains material which is subject to copyright protection. The copyright owner has no objection to the facsimile reproduction by anyone of the patent document or the patent disclosure, as it appears in the Patent and Trademark Office patent file or records, but otherwise reserves all copyright rights whatsoever.

FIELD OF INVENTION

Embodiments of the invention are generally related to software applications, and are particularly related to an application programming interface (API) for generating a JSON schema from a JSON stream.

BACKGROUND

JSON (JavaScript Object Notation) is an open and text-based data exchange format and derived from object literals of JavaScript. JSON supports primitive data types, for example string, number, Boolean and null; and structural data types, for example object and array. JSON has become a popular data exchange format in recent years due to its leaner structure compared with XML, and its ability to directly interact with JavaScript.

In an enterprise application environment, a server system, such as a stock trading system, often needs to share data in JSON format with different interfacing systems. An interfacing system or client system typically uses published data from the server system to generate a JSON schema, and uses the generated schema to pre-validate incoming data to avoid unnecessary processing loads on the client system.

SUMMARY

In accordance with an embodiment, described herein is a JSON schema generator that parses a JSON stream, determines a data type of each record of the JSON stream, and selects a corresponding record schema generation logic component based on the determined data type to build a record schema for that record. In building a record schema, each element in a record and each sub-element in one or more nested layers of the record can be examined, to determine their data types. A corresponding parser can be selected for each element and sub-element based on a determined data type to generate a schema node. The generated schema nodes can be joined together to create a record schema for the record. Each record schema can be dynamically merged together to create a schema for the JSON stream.

BRIEF DESCRIPTION OF THE FIGURES

FIG. 1 illustrates a system for generating a JSON schema from a JSON stream, in accordance with an embodiment.

FIG. 2 further illustrates a system for generating a JSON schema from a JSON stream, in accordance with an embodiment.

FIG. 3 further illustrates a system for generating a JSON schema from a JSON stream, in accordance with an embodiment.

FIG. 4 illustrates a method for generating a JSON schema from a JSON stream, in accordance with an embodiment.

DETAILED DESCRIPTION

A JSON schema is a JSON based format for defining the structure of JSON data, and can be used to validate JSON data or stream from a particular source, so that the JSON data can conform to a format required by an application on a server to avoid unnecessary processing loads on the server.

A JSON schema can be created manually or using a tool, for example JaxB, from a sample JSON data. Typically, a user or tool that creates the JSON schema needs to be aware of the structure or format of the sample JSON data, creates one or more data objects representing the data, and then convert the data objects into a JSON schema.

Accordingly, a software application that can automatically generate a JSON schema from a JSON stream without being aware of the structure of the JSON stream can be useful in a variety of environments. For example, such an application can be implemented in data control objects in an application development framework (ADF).

In accordance with an embodiment, described herein is a JSON schema generator that parses a JSON stream, determines a data type of each record of the JSON stream, and selects a corresponding record schema generation logic component based on the determined data type to build a record schema for that record. In building a record schema, each element in a record and each sub-element in one or more nested layers of the record can be examined, to determine their data types. A corresponding parser can be selected for each element and sub-element based on a determined data type to generate a schema node. The generated schema nodes can be joined together to create a record schema for the record. Each record schema can be dynamically merged together to create a schema for the JSON stream.

In accordance with an embodiment, using the system, each record of a particular data type in the JSON stream can be nested with one or more layers of a same or different data types. The system can parse the JSON stream and dynamically generate a schema that accounts for a variety of nested structures in each record without being aware of these structures before processing the JSON stream. As such, the system provides a reliable and efficient way of generating a JSON schema from a JSON stream of complicated structures.

FIG. 1 illustrates a system for generating a JSON schema from a JSON stream, in accordance with an embodiment.

As shown in FIG. 1, a JSON schema generator 104 can be deployed in a computer server environment 103 executing on one more microprocessors, as a standalone software application or as part of another software application. A user interface 110 can receive JSON data from a plurality of sources 108, (for example a URL, a file, or a string) provided by a client application 106; and can convert the JSON data into a JSON stream 112, which can represent a continuous stream of recognizable JSON entities, for example JSON object or arrays. The JSON stream can include a plurality of entities of a same data type (for example, object or array). Each entity can be considered a JSON data record of the data type.

In accordance with an embodiment, a JSON stream parser 114 can identify the data type of each JSON record, and invoke 116 a record schema generation component 118 to generate 138 a record schema 140 for that record.

As further shown in FIG. 1, the record schemas can be provided 142 to a record schema concatenator 144, which can merge a first and second record schema to create a new record schema, and use the new schema record as a base schema to dynamically merge a newly generated record schema to the base schema.

The above steps of identifying a data type of each JSON stream record, creating a record schema for that record, and merging the record schema to the base schema can be repeated until the entire JSON stream is processed. Once completed, the base schema that has incorporated different features of each of the record schemas can be provided 146 as a JSON schema file 148 for the JSON stream, and sent 150 to the client application.

FIG. 2 further illustrates a system for generating a JSON schema from a JSON stream, in accordance with an embodiment.

As shown in FIG. 2, the record schema generation component can include an array record schema generation logic component 220 and an object record schema generation logic component 222. Each record schema generation logic component can be invoked by the JSON stream parser based on the data type of a JSON stream record received at the record schema generation component.

In accordance with an embodiment, each record schema generation logic component can determine elements of a JSON stream record, determine a data type of each element, and invoke a corresponding data type parser (for example, an array parser 224, an object parser 226 or a primitive data parser 228), to generate a node schema for that element. For example, the array parser can generate a schema node for an element of an array data type. The object parser can generate a schema node for an element of an object data type. The primitive data parser can generate a node schema for each of the primitive types supported in JSON.

Each of the elements in either an array or object can be nested with one or more layers of array or object data types. Each record schema generation logic component can similarly determine the structure of each nested layer, including determining the data type of each sub-element in that layer, generate a node schema for that sub-element.

As such, the array record schema generation logic component can invoke 223, 225, 227 the array parser, the primitive data parser and the object parser to process elements or sub-elements in each nested layer of the record. Similarly, the object record schema generation logic component can invoke 229, 233, 235 the same set of parsers to process elements or sub-elements in each nested layer.

In accordance with an embodiment, once a schema node has been generated for each element or sub-element in a record, the schema nodes can be joined together to create a record schema for the record.

In accordance with an embodiment, the array record schema generation logic component and the object record schema generation logic component can use different approaches to join schema nodes to accommodate the different structures in an array or object data types.

FIG. 3 further illustrates a system for generating a JSON schema from a JSON stream, in accordance with an embodiment.

As shown in FIG. 3, the record schema concatenator 144 can load a first record schema (for example, record schema A 341) into memory as the first record is generated. When a second record schema (for example, record schema B 343) is generated, the first and second record schemas can be merged 351, 353 to create a new schema 340.

As an illustrative example, two records of a JSON stream can be {“name”: “Aminur”; “city”: “Bangalore”}, and {“name”: “Nitesh”; “email”: “abc@xyz.com”}. Each record includes two data sets, and is an entity of an object data type that represents an employee. The JSON stream parser can identity each record as an object data type, and invoke an object record schema generation logic component to generate a record schema for each record, for example {“name”, “city”} and {“name”, “email”}. The two record schemas can be merged to generate a new schema, for example {“name”, “city”, “email”}.

In accordance with an embodiment, the new schema can be used as a base schema, and each newly generated record schema (for example, record schema C 345 and record schema N 349) can be subsequently merged 355, 359 into the base schema. As each newly generated record is being merged into the base schema, the base schema can be dynamically modified to accommodate any structural differences in each record schema.

The steps of identifying data types, and creating and merging of record schemas, can be repeated until the entire JSON stream is processed.

FIG. 4 illustrates a method for generating a JSON schema from a JSON stream, in accordance with an embodiment.

As shown in FIG. 4, at step 411, a JSON stream that includes one or more records of a same data type is received from a client application. Each of the one or more records can be one of an array data type or an object data type.

At step 413, a JSON stream parser can be used to parse the JSON stream, to determine a data type of each record.

At step 415, a record schema generation logic component can be selected for each record based on the determined data type, to generate a record schema for that record, wherein generating that record schema includes creating a schema node for each of the elements and sub-elements in the record, and joining each schema node together.

At step 417, a record schema concatenator can be invoked to merge a first record schema and a second record schema to create a new record schema, wherein the new record schema can be used as a base schema for incorporating any new record schemas.

At step 419, a schema generated for each of the remaining records is merged into the base schema until the entire JSON stream is processed, and the base schema is provided to the client application as the schema for the JSON stream.

Exemplary Implementation

In accordance with an embodiment, a system for generating a JSON schema from a JSON stream can be implemented using a plurality of application programming interface (API) objects. The API objects and sample code provided below are for illustration purposes and should not be construed to limit any of the embodiments of the invention.

A user interface can be implemented using the following API objects to enable users to provide JSON streams to the system.

APIs to set JSON streams  - public void setJsonSource(File file)  - public void setJsonSource(InputStream inputStream)  - public void setJsonSource(String string) APIs to generate schemas (with optional choice of formatting)  - public String generate( )  - public String generate(PrettyPrinter printer)  - public void generate(OutputStream out)  - public void generate(OutputStream out, PrettyPrinter printer

A JSON stream parser can be implemented using the following API objects to parse the JSON streams and identify the data types of each of the records in the JSON streams. These API objects can include JSON data that is deeply nested. For example, a department object can include an employee object, which further includes an address object. Once the structure of a JSON element in a JSON record is identified, a schema node can be generated using the following API objects.

Interface to parse the input stream (JSON data) provided by users  - JsonDataReader In-built classes to parse standard JSON data types  - JsonArrayDataParser  - JsonObjectDataParser  - JsonPrimitiveDataParser Factory class to select a parser based on JSON data types  - JsonParserFactory

In accordance with an embodiment, the generated schema nodes can be represented using the following API objects.

Schema structure is represented by  - JsonData Supported schema node type is defined in  - JsonPrimitiveType Class to represent schema node type  - JsonPrimitiveData  - JsonObjectData  - JsonArrayData

The following sample code illustrates how to use the above-described API objects to construct an application to generate JSON schema from a JSON stream.

The first example illustrates a JSON string that includes a plurality of JSON records, where each record is a JSON object.

JsonSchemaGenerator generator = new JsonSchemaGenerator( ); /** Example 1 with input source as String {  “tags”: [   {    “f3”:1,    “f2”:2   },   {    “f2”:1,    “f3”:3   },   {    “f1”:1,    “f3”:2,    “f4”:4   }   ] } */ String JsonString = “{\n\“tags\”: [{\“f3\”:1,\“f2\”:2},{\“f2\”:1,\“f3\”:3}, {\“f1\”:1,\“f3\”:2,\“f4\”:4}]\n}”; generator.setJsonSource(jsonString); String outSchema = generator.generate( ); System.out.println(outSchema); /**Generated JSON schema*/ {  “$schema”: “http://www.oracle.com/schema”,  “id”: “http://www.oracle.com/schema”,  “type”: “object”,  “properties”: {  “tags”:{   “type”: “array”,   “items”: {    “type”: “object”,    “properties”: {     “f1”:{      “type”: “number”     },     “f3”:{      “type”: “number”     },     “f2”:{      “type”: “number”     },     “f4”:{      “type”: “number”     }    }   }   “required”: [“f3”]   }  } }

Embodiments of the present invention may be conveniently implemented using one or more conventional general purpose or specialized digital computer, computing device, machine, or microprocessor, including one or more processors, memory and/or computer readable storage media programmed according to the teachings of the present disclosure. Appropriate software coding can readily be prepared by skilled programmers based on the teachings of the present disclosure, as will be apparent to those skilled in the software art.

In some embodiments, the present invention includes a computer program product which is a non-transitory storage medium or computer readable medium (media) having instructions stored thereon/in which can be used to program a computer to perform any of the processes of the present invention. Examples of the storage medium can include, but is not limited to, any type of disk including floppy disks, optical discs, DVD, CD-ROMs, microdrive, and magneto-optical disks, ROMs, RAMs, EPROMs, EEPROMs, DRAMs, VRAMs, flash memory devices, magnetic or optical cards, nanosystems (including molecular memory ICs), or any type of media or device suitable for storing instructions and/or data.

The foregoing description of embodiments of the present invention has been provided for the purposes of illustration and description. It is not intended to be exhaustive or to limit the invention to the precise forms disclosed. Many modifications and variations will be apparent to the practitioner skilled in the art. The embodiments were chosen and described in order to best explain the principles of the invention and its practical application, thereby enabling others skilled in the art to understand the invention for various embodiments and with various modifications that are suited to the particular use contemplated. 

What is claimed is:
 1. A system for automatically generating a JSON schema from a JSON stream, including a computer including one or more microprocessors; a software application, executing on the computer, that includes a plurality of data type parsers, a plurality of record schema generation logic components, and a record schema concatenator; wherein when receiving a JSON stream, the software application operates to determine a data type of each record in the JSON stream, select, based on the determined data type, a corresponding record schema generation logic component to generate a record schema for that record, and use the record schema concatenator to join each record schema to create a schema for the JSON stream.
 2. The system of claim 1, wherein the data type is an array data type or an object data type supported by JSON.
 3. The system of claim 1, wherein one or more records in the JSON stream are nested with one or more layers, each layer including sub-elements of particular JSON data types.
 4. The system of claim 1, wherein the plurality of record schema generation components include an array record schema generation logic component, and an object record schema generation logic component.
 5. The system of claim 1, wherein generating a record schema for a record including determining a data type of each element and each sub-element in a nested layer in the record, selecting a corresponding data type parser to generate a schema node based on the determined data type, and joining each schema node to create a record schema using the corresponding record schema generation logic component.
 6. The system of claim 1, wherein joining each record schema to create the schema for the JSON stream include merging a first and a second record schemas to create a new schema as a base schema, and dynamically merging each newly generated record schema into the base schema.
 7. The system of claim 6, further including dynamically modifying the base schema with each newly generated record schema to accommodate any structural differences in the newly generated record schema.
 8. The system of claim 1, further comprising a user interface that accepts JSON data from a plurality of sources, including a file, a string and a URL.
 9. The system of claim 1, wherein the software application needs not to be aware of the structure of the JSON stream before processing the JSON stream.
 10. The system of claim 1, wherein the steps of identifying data types, and creating and merging of record schemas are repeated until the entire JSON stream is processed.
 11. A method for automatically generating a JSON schema from a JSON stream, including receiving a JSON stream at a software application executing on the computer, wherein the software application includes a plurality of data type parsers, a plurality of record schema generation logic components, and a record schema concatenator; determining a data type of each record in the JSON stream; selecting, based on the determined data type, a corresponding record schema generation logic component to generate a record schema for that record; and using the record schema concatenator to join each record schema to create a schema for the JSON stream.
 12. The method of claim 11, wherein the data type is an array data type or an object data type supported by JSON.
 13. The method of claim 11, wherein one or more records in the JSON stream are nested with one or more layers, each layer including sub-elements of particular JSON data types.
 14. The method claim 11, wherein the plurality of record schema generation components include an array record schema generation logic component, and an object record schema generation logic component.
 15. The method of claim 11, wherein generating a record schema for a record including determining a data type of each element and each sub-element in a nested layer in the record, selecting a corresponding data type parser to generate a schema node based on the determined data type, and joining each schema node to create a record schema using the corresponding record schema generation logic component.
 16. The method of claim 11, wherein joining each record schema to create the schema for the JSON stream include merging a first and a second record schemas to create a new schema as a base schema, and dynamically merging each newly generated record schema into the base schema.
 17. The method of claim 16, further including dynamically modifying the base schema with each newly generated record schema to accommodate any structural differences in the newly generated record schema.
 18. The method of claim 11, further comprising a user interface that accepts JSON data from a plurality of sources, including a file, a string and a URL.
 19. The method of claim 11, wherein the software application needs not to be aware of the structure of the JSON stream before processing the JSON stream.
 20. A non-transitory computer readable storage medium, including instructions stored thereon which when read and executed by one or more computers cause the one or more computers to perform the steps comprising: receiving a JSON stream at a software application executing on the computer, wherein the software application includes a plurality of data type parsers, a plurality of record schema generation logic components, and a record schema concatenator; determining a data type of each record in the JSON stream; selecting, based on the determined data type, a corresponding record schema generation logic component to generate a record schema for that record; and using the record schema concatenator to join each record schema to create a schema for the JSON stream. 